package com.example.controller;

import com.example.model.Contact;
import com.example.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contacts")
@CrossOrigin(origins = "http://localhost:3000")
public class ContactController {
    @Autowired
    private ContactService contactService;

    @GetMapping
    public List<Contact> getAllContacts() {
        return contactService.getAllContacts();
    }

    @PostMapping
    public void addContact(@RequestBody Contact contact) {
        contactService.addContact(contact);
    }

    @PutMapping("/{name}")
    public void updateContact(@PathVariable String name, @RequestBody Contact contact) {
        contactService.updateContact(contact);
    }

    @DeleteMapping("/{name}")
    public void deleteContact(@PathVariable String name) {
        contactService.deleteContact(name);
    }
}
